package com.yizhubao.app.fragments;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.xw.repo.XEditText;
import com.yizhubao.app.R;
import com.yizhubao.app.activitys.search.SearchActivity;
import com.yizhubao.app.base.BaseFragment;
import com.yizhubao.app.customviews.RiseNumber.RiseNumberTextView;
import com.yizhubao.app.customviews.VerificationCodeText;
import com.yizhubao.app.presenters.AssignorPresenter;
import com.yizhubao.app.presenters.SendCodePresenter;
import com.yizhubao.app.views.AssignorView;
import com.yizhubao.app.views.SendCodeView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import h.framework.pacy.utils.DateUtil;
import h.framework.pacy.utils.LogUtil;
import h.framework.pacy.utils.StringUtil;
import h.framework.pacy.utils.ToastUtil;

/**
 * 创建人: PACY
 * 创建时间: 6/4/21 10:25 AM
 * 邮箱: pengwwe457621023@qq.com
 * 类描述: 搜索fragment
 */
public class SearchFragment extends BaseFragment implements SendCodeView, AssignorView {
    @BindView(R.id.riseNumber)
    RiseNumberTextView numberTextView;

    @BindView(R.id.getCode_vc)
    VerificationCodeText getCode_vc;

    @BindView(R.id.phone_et)
    XEditText phone_et;
    @BindView(R.id.code_et)
            EditText code_et;

    SendCodePresenter codePresenter;
    AssignorPresenter assignorPresenter;

    public static SearchFragment newInstance(String title) {

        Bundle args = new Bundle();
        args.putString("title", title);
        SearchFragment fragment = new SearchFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        numberTextView.setDuration(500);
        phone_et.setSeparator(" ");
        phone_et.setPattern(new int[]{3,4,4});
        phone_et.setDisableClear(true);
        getCode_vc.setEnableType(false);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            numberTextView.withNumber(DateUtil.dateDiff("2021-03-01 00:00:00")).start();
        }
    }

    @Override
    protected void initData() {
        codePresenter=new SendCodePresenter();
        codePresenter.attachView(this);
        assignorPresenter=new AssignorPresenter();
        assignorPresenter.attachView(this);

        phone_et.setOnXTextChangeListener(new XEditText.OnXTextChangeListener() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LogUtil.i(s.length()+"");
                if(s.length()>12){
                    getCode_vc.setEnableType(true);
                }else{
                    getCode_vc.setEnableType(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 发送验证码请求
     */
    public void sendCodeRequest(){
        String phone=phone_et.getText().toString().replaceAll(" ","");
        if(StringUtil.isEmpty(phone)){
            ToastUtil.showShort("请输入完整的手机号");
            return;
        }
        if(phone.length()!=11){
            ToastUtil.showShort("请输入完整的手机号");
            return;
        }
        Map<String,Object> map=new HashMap<>();
        map.put("type","1");
        map.put("phone",phone);
        codePresenter.sendCodeRequest(map);
    }


    private void sendAssignorRequest(){
        String phone=phone_et.getText().toString().replaceAll(" ","");
        if(StringUtil.isEmpty(phone)){
            ToastUtil.showShort("请输入完整的手机号");
            return;
        }
        if(phone.length()!=11){
            ToastUtil.showShort("请输入完整的手机号");
            return;
        }
        String code=code_et.getText().toString();
        if(StringUtil.isEmpty(phone)){
            ToastUtil.showShort("请输入完整的验证码");
            return;
        }
        if(phone.length()<6){
            ToastUtil.showShort("请输入完整的验证码");
            return;
        }
        Map<String,Object> map=new HashMap<>();
        map.put("code",code);
        map.put("phone",phone);
        showLoading();
        assignorPresenter.sendAssignor(map);
    }

    @OnClick(value = {R.id.getCode_vc,R.id.commit_btn})
    public void click(View v){
        if(v.getId()==R.id.getCode_vc){
            sendCodeRequest();
        }else if(v.getId()==R.id.commit_btn){
            sendAssignorRequest();
        }
    }

    @Override
    public int setLayoutResourceID() {
        return R.layout.search_fragment;
    }

    @Override
    public void onCodeSuccess(boolean send) {
        if(send){
            getCode_vc.setLenght(60*1000);
            getCode_vc.start();
        }
    }

    @Override
    public void onAssignorSuccess(boolean send) {
            Bundle bundle=new Bundle();
            bundle.putBoolean("has",send);
            startActivity(getActivity(), SearchActivity.class,bundle);
        phone_et.setText("");
            code_et.setText("");
    }
}
